<?php

namespace App\Admin\Controllers\Common;

use App\Admin\Models\SiteStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Models\Brand;

class BrandController extends Controller
{
    /**
     * 获取品牌管理列表
     */
    public function getlist(Request $request)
    {
        $list = Brand::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取品牌管理详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = Brand::where(['id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增品牌管理
     */
    public function dopostadd(Request $request)
    {
        $brand_name = $request->brand_name ?? '';

        if ($brand_name=='') {
            return response()->json(['code' => '001', 'msg' => '品牌名称不能为空']);
        }
        $isexist = Brand::where(['brand_name' => $brand_name])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '品牌名称不允许重复']);
        }

        $data = ['brand_name'=>$brand_name];
        $data['create_at'] = time();
        $data['update_at'] = time();
        Brand::create($data);
        return response()->json(['code' => '000', 'msg' => '新增品牌成功']);
    }


    public function dopostedit(Request $request)
    {
        $id=intval($request->id) ?? 0;
        $brand_name = $request->brand_name ?? '';

        if ($brand_name=='') {
            return response()->json(['code' => '001', 'msg' => '品牌名称不能为空']);
        }
        $isexist = Brand::where(['brand_name' => $brand_name,['id', '<>',$id]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '品牌名称不允许重复']);
        }

        $info = Brand::where([['id', $id],['is_del',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $data = ['brand_name'=>$brand_name];
        $data['update_at'] = time();
        Brand::where(['id' => $id])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑品牌成功']);
    }
}
